package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.utils.QiniuUtils;
import com.atguigu.constant.RedisConstant;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.SetmealService;
import com.atguigu.vo.PageResult;
import com.atguigu.vo.QueryPageBean;
import com.atguigu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

/**
 * @program: meinian_parent
 * @author: Mr.Zhuo
 * @create: 2022-02-14 21:17
 **/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;


    @GetMapping("/findTravelgroupIdBySetmealId")
    public Result findTravelgroupIdBySetmealId(@RequestParam("id") Integer id) {
        try {
            List<Integer> travelgroupIds = setmealService.findTravelgroupIdBySetmealId(id);
            return new Result(true, MessageConstant.QUERY_TRAVELITEM_SUCCESS, travelgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }

    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = null;
        try {
            pageResult = setmealService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestParam("travelgroupIds") Integer[] travelgroupIds, @RequestBody Setmeal setmeal) {
        try {
            setmealService.update(travelgroupIds, setmeal);
            return new Result(true, MessageConstant.EDIT_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_TRAVELITEM_FAIL);
        }
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        try {
            setmealService.delete(id);
            return new Result(true, MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }
    @PostMapping("/add")
    public Result add(@RequestParam("travelgroupIds") Integer[] travelgroupIds, @RequestBody Setmeal setmeal) {
        try {
            setmealService.add(travelgroupIds, setmeal);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findByID(@RequestParam("id") Integer id) {
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    /**
     *  然后查询跟团游数据回显
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<Setmeal> list = setmealService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }


    }

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile imgFile) {
        try {
            // 要传输的数据文件
            byte[] bytes = imgFile.getBytes();
            // 文件的真实姓名
            String fileOriginalFilename = imgFile.getOriginalFilename();
            // .的最后的位置
            int indexOf = fileOriginalFilename.lastIndexOf(".");
            // 获取文件名的后缀
            String suffix = fileOriginalFilename.substring(indexOf);
            // 真正储存到七牛云上面的文件姓名
            String filename = UUID.randomUUID().toString() + suffix;

            QiniuUtils.upload2Qiniu(bytes, filename);

            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, filename);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

}
